package com.xlf.service.blog.controller.admin;

import com.xlf.common.auth.util.SecurityUtils;
import com.xlf.common.core.pojo.mode.AjaxResult;
import com.xlf.common.core.pojo.mode.BaseController;
import com.xlf.service.blog.domain.Article;
import com.xlf.service.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/blog/admin/article")
public class AdminArticleController extends BaseController {

    @Autowired
    ArticleService articleService;

    /**
     * 获取文章列表
     * @param article
     * @return
     */
    @PreAuthorize("@ss.hasPermi('blog:admin:article:list')")
    @GetMapping("/list")
    public AjaxResult list(Article article){
        startPage();
        return success(getDataTable(articleService.getPage(article)));
    }

    /**
     * 获取文章
     * @param articleId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('blog:admin:article:query')")
    @GetMapping("/{id}")
    public AjaxResult query(@PathVariable(value = "id") @Validated @NotNull(message = "文章不能为空") Long articleId){
        return success(articleService.getArticle(articleId));
    }

    /**
     * 修改文章
     */
    @PutMapping("/saveOrUpdate")
    @PreAuthorize("@ss.hasPermi('blog:admin:article:saveOrUpdate')")
    public AjaxResult saveOrUpdate(@Validated @RequestBody Article article) {
        if(article.getUserId() == null)
            article.setUserId((Long) SecurityUtils.getUserId());
        articleService.saveOrUpdate(article);
        return success();
    }

    /**
     * 删除文章
     * @return
     */
    @PreAuthorize("@ss.hasPermi('blog:admin:article:delete')")
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestBody List<Long> ids){
        articleService.removeBatchByIds(ids);
        return success("删除成功！");
    }

}
